package View.SaltPepperDialog;

import View.MainFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Point2D;

public class SaltPepperDialog extends JDialog implements ActionListener {
    MainFrame parentFrame;
    JTextField jtfProbabilty = new JTextField();
    JButton jbtnOK = new JButton("OK");
    JButton jbtnCancel = new JButton("Cancel");

    boolean closeFlag;

    static final String OK_COMMAND = "OK_COMMAND";
    static final String CANCEL_COMMAND = "CANCEL_COMMAND";

    double p;


    public SaltPepperDialog(MainFrame parentFrame) {
        this.parentFrame = parentFrame;
        setLocationRelativeTo(parentFrame);
        setSize(250, 170);
        setResizable(false);
        setTitle("Salt and Pepper parameters");
        setModal(true);
        setVisible(false);

        jbtnOK.addActionListener(this);
        jbtnOK.setActionCommand(OK_COMMAND);

        jbtnCancel.addActionListener(this);
        jbtnCancel.setActionCommand(CANCEL_COMMAND);

        GridBagLayout gb = new GridBagLayout();
        setLayout(gb);

        JLabel jlProbability = new JLabel("Set probability [0;1]: ");
        gb.setConstraints(jlProbability, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 0, 0), 0, 0));
        add(jlProbability);
        gb.setConstraints(jtfProbabilty, new GridBagConstraints(0, 1, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 0), 0, 0));
        add(jtfProbabilty);



        gb.setConstraints(jbtnOK, new GridBagConstraints(0, 4, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 10), 0, 0));
        add(jbtnOK);
        gb.setConstraints(jbtnCancel, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 10), 0, 0));
        add(jbtnCancel);

    }

    public boolean runDialog() {
        closeFlag = false;
        setVisible(true);
        return  closeFlag;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals(OK_COMMAND)) {
            boolean errFlag = false;
            try
            {
                p = Double.valueOf(jtfProbabilty.getText());
            }
            catch (NumberFormatException nfe)
            {
                errFlag = true;
            }
            if (errFlag) {
                JOptionPane.showMessageDialog(this, "Error parameters.", "Error!", JOptionPane.ERROR_MESSAGE);
                return;
            }
            if (p < 0 || p > 1) {
                JOptionPane.showMessageDialog(this, "Probability must be in [0,1].", "Error!", JOptionPane.ERROR_MESSAGE);
                return;
            }
            closeFlag = true;
            setVisible(false);

        }
        if (e.getActionCommand().equals(CANCEL_COMMAND)) {
            setVisible(false);
        }

    }

    public double getProb() {
        return p;
    }

}
